package com.netvine.iias;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.netvine.iias.run.hCNetSDK;
import static com.netvine.iias.run.lUserID;

@Component
public class PtzControllUtil {
    private static final Logger log = LoggerFactory.getLogger(PtzControllUtil.class);
    private int previewHandle;
    private boolean isReplay = false;

    @Autowired
    private run run;

    public void preview() {
        if (isReplay) {
            return;
        }
        //获取通道号
        int iChannelNum = 34;//通道号
        if (iChannelNum == -1) {
            log.info("获取通道号失败");
            return;
        }
        HCNetSDK.NET_DVR_PREVIEWINFO strClientInfo = new HCNetSDK.NET_DVR_PREVIEWINFO();
        strClientInfo.read();
        strClientInfo.hPlayWnd = null;  //窗口句柄，从回调取流不显示一般设置为空
        strClientInfo.lChannel = iChannelNum;  //通道号
        strClientInfo.dwStreamType=0; //0-主码流，1-子码流，2-三码流，3-虚拟码流，以此类推
        strClientInfo.dwLinkMode=0; //连接方式：0- TCP方式，1- UDP方式，2- 多播方式，3- RTP方式，4- RTP/RTSP，5- RTP/HTTP，6- HRUDP（可靠传输） ，7- RTSP/HTTPS，8- NPQ
        strClientInfo.bBlocked=1;  //0- 非阻塞取流，1- 阻塞取流
        strClientInfo.write();
        int lPreviewHandle = hCNetSDK.NET_DVR_RealPlay_V40(lUserID, strClientInfo, null , null);
        if (lPreviewHandle<=-1)
        {
            int error;
            error=hCNetSDK.NET_DVR_GetLastError();
            log.info("预览失败，错误码：{}", error);
            return;
        }
        previewHandle = lPreviewHandle;
        isReplay = true;
    }

    public void setPreset() {
        hCNetSDK.NET_DVR_PTZPreset(previewHandle, HCNetSDK.SET_PRESET, 1);
    }

    public void gotoPreset() {
        hCNetSDK.NET_DVR_PTZPreset(previewHandle, HCNetSDK.GOTO_PRESET, 1);
    }

    public void stopPreview() {
        //如果在预览,停止预览,关闭窗口
        Boolean bStopSaveVideo = hCNetSDK.NET_DVR_StopSaveRealData(previewHandle);
        if (bStopSaveVideo == false) {
            int iErr = hCNetSDK.NET_DVR_GetLastError();
            System.out.println("停止录像失败" + iErr);
            return;
        }
        System.out.println("停止录像成功");
        hCNetSDK.NET_DVR_StopRealPlay(previewHandle);
        isReplay = false;
    }

    public void selZoom() throws InterruptedException {
        if (hCNetSDK == null) {
            run.createSDKInstance();
        }
        //初始化
        hCNetSDK.NET_DVR_Init();
        boolean loginFlag = run.loginDefault();
        if (loginFlag) {
            preview();
            setPreset();
            //int realHandle = hCNetSDK.NET_DVR_RealPlay(lUserID,netDvrClientinfo);
            HCNetSDK.NET_DVR_POINT_FRAME frame = new HCNetSDK.NET_DVR_POINT_FRAME();
            frame.xTop = 10;
            frame.xBottom = 20;
            frame.yTop = 10;
            frame.yBottom = 20;
            boolean res = hCNetSDK.NET_DVR_PTZSelZoomIn(previewHandle, frame);
            log.info("放大结果：{}", res);
            hCNetSDK.NET_DVR_PTZControlWithSpeed(previewHandle, 23, 0, 4);
            Thread.sleep(2000);
            hCNetSDK.NET_DVR_PTZControlWithSpeed(previewHandle, 23, 1, 4);
            gotoPreset();
            stopPreview();
            hCNetSDK.NET_DVR_Logout_V30(lUserID);
        }
    }
}
